/* This is the JSON data processing end for homepage */

import { Homepage_Card, Homepage_Reminder } from './hp_card.js';
import { VNS_panel } from './hp_panel.js';

const homepage_vns_url = "./assets/json/vns_collection.json";
const homepage_cards_url = "./assets/json/anicard_dataset.json";
const _HEAD_SIGN = "HEAD";
const homepage_status = {
    visible_display_queue: new DisplayQueue("visible display queue"),
    hidden_display_queue: new DisplayQueue("hidden display queue"),
    filtering_tag_queue: ["", []],
    filtering_keyword_queue: []
}

class DisplayQueue {
    constructor(init_first_member, name) {
        this._first_member = first_member;
        this._name = name + "";
        this._queue  = { head: [new DisplayQueueMember({ VNS_tag: _HEAD_SIGN }), 0] };
    }

    keys () {
        return Object.keys(this._queue);
    }

    reset_first_member (VNS_tag) {
        if(this.keys().length === 1 || this.keys().indexOf(VNS_tag) < 0) {
            console.error("You can not set a nonexistent member to the \'first member\'");
            return false;
        }

        this._first_member = VNS_tag;
        return true;
    }
    
    reset_link (prev_VNS_tag, next_VNS_tag) {
        if(this.keys().length === 1 
            || this.keys().indexOf(prev_VNS_tag) < 0 
            || !(this.keys().indexOf(next_VNS_tag) < 0 || next_VNS_tag === "")
        ) {
            console.error("You can not set nonexistent member\(s\) to the a new link.");
            return false;
        }

        this._queue[prev_VNS_tag][0].set_next_tag (next_VNS_tag);
        return true;
    }

    get_next_tag (VNS_tag) {
        if(this.keys().length === 1 || this.keys().indexOf(VNS_tag) < 0) {
            console.error("You can not read an empty queue nor a nonexistent display!");
            return false;
        }
        return this._queue[VNS_tag][0].get_next_tag();
    }

    is_first_member (VNS_tag) {
        VNS_tag = VNS_tag + "";
        if(this.keys().indexOf(VNS_tag) > -1 && this._queue["head"].get_next_tag() === VNS_tag) {
            return true;
        }
        return false;
    }

    append (display_member, next_tag = "", first = false) {
        first = first || false;
        let VNS_tag = display_member.get_VNS_tag();
        if(this.keys().indexOf(VNS_tag) > -1) {
            console.error(`VNS_tag: ${VNS_tag} has already existed in the queue.`);
            return -1;
        } 
        let count = display_member._count_cards();
        this._queue[VNS_tag] = [display_member, count]; // single queue template
        if(first) {
            this.reset_first_member(VNS_tag);
        }
        return this.keys().length - 1;
    }

    // only step out of the link
    // *** shouldn't be used to remove a display member from the queue! ***
    remove (VNS_tag) {
        VNS_tag = VNS_tag + "";
        if(this.keys().indexOf(VNS_tag) < 0) {
            console.log(`VNS_tag: ${VNS_tag} doesn't exist in the queue.`);
            return false;
        }
        
        let prev_display_tag = "head";
        let next_display_tag = this.get_next_tag(VNS_tag);
        while (this.get_next_tag(prev_display_tag) !== VNS_tag) {
            prev_display_tag = this.get_next_tag(prev_display_tag);
        }
        this.reset_link(prev_display_tag, next_display_tag);
        this.reset_link(VNS_tag, "");

        if(this.is_first_member(VNS_tag)) {
            this.reset_first_member(next_display_tag);
        }
        return VNS_tag;
    }

    pushCard (card_subject) {
        let VNS_tag = card_subject._get_param("VNS_tag");
        if(this.keys().indexOf(VNS_tag) < 0) {
            console.error(`We don't have VNS_tag: ${VNS_tag} here in the queue.`);
            return false;
        }
        this._queue[VNS_tag][0]._pushCard(card_subject);
        this._queue[VNS_tag][1]++;
    }

    delCard (VNS_tag, card_id) {
        if(this.keys().indexOf(VNS_tag) < 0) {
            console.error(`We don't have VNS_tag: ${VNS_tag} here in the queue.`);
            return false;
        }
        this._queue[VNS_tag][0]._delCard(card_id);
        this._queue[VNS_tag][1]--;
    }
}

class DisplayQueueMember extends Homepage_Reminder {
    constructor ({ VNS_tag, VNS_desc, VNS_num }, cards_list = [], next_tag = "") {
        super ({ VNS_tag, VNS_desc, VNS_num });
        this._cards_list = cards_list;
        this._next_tag = next_tag;
    }

    get_VNS_tag () {
        return this._VNS_tag || "";
    }

    set_next_tag (VNS_tag) {
        if(isNaN(VNS_tag)) { 
            return false;
        }
        this._head = VNS_tag;
        return true;
    }

    get_next_tag () {
        return this._next_tag;
    }

    _set_head () {
        if(this._VNS_tag !== "") {
            console.error("This is not an empty link node.");
            return false;
        }
        this._VNS_tag = _HEAD_SIGN;
        return true;
    }

    is_head () {
        if(this._VNS_tag === _HEAD_SIGN) {
            return true;
        }
        return false;
    }

    is_tail () {
        if(this._VNS_tag === "") {
            return true;
        }
        return false;
    }

    _get_card_id_list () {
        return Array.from(this._cards_list, x => Number(x._get_param("card_id")));
    }

    _count_cards () {
        return this._cards_list.length;
    }

    _pushCard (card_subject) {
        let card_id = card_subject._get_param("card_id");
        if(card_subject._get_param("VNS_tag") !== this._VNS_tag || (this._get_card_id_list()).indexOf(card_id) > -1) {
            return false;
        }
 
        this._cards_list.push(card_subject);
        this._cards_list.sort((a, b) => Number(a._get_param("card_id") - Number(b._get_param("card_id"))));
        return true;
    }

    _delCard (card_id) {
        card_id = Number(card_id) || 0;
        let id_list = this._get_card_id_list();
        let card_No_inArray = id_list.indexOf(card_id);
        if((card_No_inArray < 0) || Number(card_id) <= 0) {
            console.error("This card id doesn\'t exist.");
            return -1;
        }

        this._cards_list.splice(card_No_inArray, 1);
        return card_No_inArray;
    }
}


function hp_JSON_processing (filtering_tag = ["", []], filtering_keyword = []) {
    
    return {
        visible_display_queue: homepage_status["visible_display_queue"],
        hidden_display_queue: homepage_status["hidden_display_queue"]
    };
}

function _create_display_subjects (display_url) {

    $.getJSON(display_url, json => {
        json.forEach((json_item, i, jsonArr) => {
            let display_reminder_subject = new Homepage_Reminder({
                VNS_tag: json_item["VNS_tag"],
                VNS_desc: json_item["VNS_desc"],
                VNS_num: json_item["VNS_num"]
            });
            
            
        });
    });
}